package org.ring.rinject.test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;
import org.ring.rinject.Rinjector;
import org.ring.rinject.test.zoo.Animal;
import org.ring.rinject.test.zoo.Bone;
import org.ring.rinject.test.zoo.Cat;
import org.ring.rinject.test.zoo.Dog;
import org.ring.rinject.test.zoo.Fish;
import org.ring.rinject.test.zoo.Food;

public class ZooTest2 {
	Rinjector rinjector;

	@Before
	public void setUp() {
		rinjector = new Rinjector();

		try {
			Fish fish = new Fish();
			Bone bone = new Bone();

			rinjector.map(Cat.class).map(Dog.class).map(fish).map(bone);
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void test1() {
		Animal cat = new Cat();
		Animal dog = new Dog();

		try {
			Food catFood1 = rinjector.env(cat).concrete(Food.class).target();
			Food catFood2 = rinjector.env(cat).concrete("food").target();
			Food catFood3 = rinjector.env(Cat.class).concrete(Food.class)
					.target();

			Food dogFood1 = rinjector.env(Dog.class).concrete("food")
					.target();
			Food dogFood2 = rinjector.env("dog").concrete(Food.class)
					.target();
			Food dogFood3 = rinjector.env("dog").concrete("food").target();

			assertTrue("Cat don't eat this: " + catFood1, cat.eat(catFood1));
			assertTrue("Cat don't eat this: " + catFood2, cat.eat(catFood2));
			assertTrue("Cat don't eat this: " + catFood3, cat.eat(catFood3));
			assertTrue("Dog don't eat this: " + dogFood1, dog.eat(dogFood1));
			assertTrue("Dog don't eat this: " + dogFood2, dog.eat(dogFood2));
			assertTrue("Cat don't eat this: " + dogFood3, dog.eat(dogFood3));

		} catch (Exception e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void test2() {
		Animal cat = new Cat();
		Animal dog = new Dog();

		try {
			Food catFood1 = rinjector.findObject(cat, Food.class);
			Food catFood2 = rinjector.findObject(cat, "food");
			Food catFood3 = rinjector.findObject(Cat.class, Food.class);

			Food dogFood1 = rinjector.findObject(Dog.class, "food");
			Food dogFood2 = rinjector.findObject("dog", Food.class);
			Food dogFood3 = rinjector.findObject("dog", "food");

			assertTrue("Cat don't eat this: " + catFood1, cat.eat(catFood1));
			assertTrue("Cat don't eat this: " + catFood2, cat.eat(catFood2));
			assertTrue("Cat don't eat this: " + catFood3, cat.eat(catFood3));
			assertTrue("Dog don't eat this: " + dogFood1, dog.eat(dogFood1));
			assertTrue("Dog don't eat this: " + dogFood2, dog.eat(dogFood2));
			assertTrue("Cat don't eat this: " + dogFood3, dog.eat(dogFood3));

		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
}
